// CalcualtionPage.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "CalculationPage.h"
#include "CharplanDoc.h"


// CCalcualtionPage-Dialogfeld

IMPLEMENT_DYNCREATE(CCalculationPage, CDialog)

CCalculationPage::CCalculationPage(CWnd* pParent /*=NULL*/)
        : CDialog()
{

}

CCalculationPage::~CCalculationPage()
{
}

void CCalculationPage::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CCalculationPage, CDialog)
END_MESSAGE_MAP()


//////////////////////////////////////////////////////////////////////////
CPage1* CCalculationPage::GetPage()
{
    // One level UP
    CWnd* p = GetParent();
    ASSERT(p); // no parent
    ASSERT(strcmp(p->GetRuntimeClass()->m_lpszClassName, "CCalculationSheet")  == 0);     // not the right parent

    p = p->GetParent();
    ASSERT(p); // no parent
    ASSERT(strcmp(p->GetRuntimeClass()->m_lpszClassName, "CPage1")  == 0);     // not the right parent

    // get the document of the view
    return (CPage1*)p;
}


//////////////////////////////////////////////////////////////////////////
CCharplanDoc* CCalculationPage::GetDocument()
{
    return GetPage()->GetDocument();
}


//////////////////////////////////////////////////////////////////////////
int  CCalculationPage::GetOption(LPCTSTR name, int def)
{
    return GetPage()->GetDocument()->GetOption(name,def);
}



//////////////////////////////////////////////////////////////////////////
bool CCalculationPage::SetOption(LPCTSTR name, int val)
{
    CCharplanDoc* pdoc = GetPage()->GetDocument();
    bool realchanged = (val != pdoc->GetOption(name));
    if (realchanged) pdoc->SetOption(name,val);

    return realchanged;
}

//////////////////////////////////////////////////////////////////////////
BOOL CCalculationPage::OnInitDialog()
{
    CDialog::OnInitDialog();

    SetupToolTips();

    return TRUE;
}

//////////////////////////////////////////////////////////////////////////
void CCalculationPage::SetupToolTips()
{
    mToolTips.Create(this);
    mToolTips.SetDelayTime(TTDT_AUTOPOP,0);
    mToolTips.SetDirection(PPTOOLTIP_TOPEDGE_LEFT);
    mToolTips.SetBehaviour(PPTOOLTIP_CLOSE_LEAVEWND);
    mToolTips.SetDelayTime(TTDT_INITIAL,500);
}


//////////////////////////////////////////////////////////////////////////
void CCalculationPage::EditCtrlChanged(CEdit* pc_ctrl,LPCTSTR value_name, CPage1::enum_hard_caps cap, bool correction)
{
    EditCtrlChanged(pc_ctrl,value_name, GetPage()->GetCap(cap), correction);
}



//////////////////////////////////////////////////////////////////////////
void CCalculationPage::EditCtrlChanged(CEdit* pc_ctrl,LPCTSTR value_name, int n_cap, bool correction)
{
    CString temp;
    pc_ctrl->GetWindowText(temp);
    int n = _tstoi(temp);
    int nc = CLAMP(n,0, n_cap);

    bool realchanged = (nc != GetOption(value_name,0));
    if (realchanged) SetOption(value_name,nc);

    if (n!=nc && correction)
    {
        temp.Format(_T("%i"),nc);
        pc_ctrl->SetWindowText(temp);
    }
}


//////////////////////////////////////////////////////////////////////////
BOOL CCalculationPage::PreTranslateMessage(MSG* pMsg)
{
    mToolTips.RelayEvent(pMsg);
    return CDialog::PreTranslateMessage(pMsg);
}

